﻿using System.Collections.Generic;
using System.Linq;
using VotesResults.Models;

namespace VotesResults.Daos.EntityFramework
{
    public class QuestionDao : IQuestionDao
    {
        public List<Question> Load()
        {
            using (VotesResultsEntities db = new VotesResultsEntities())
                return db.Question.ToList();
        }

        public List<Question> Load(int voteId)
        {
            using (VotesResultsEntities db = new VotesResultsEntities())
                return (
                    from q in db.Question
                    from rvq in db.RelVoteQuestion
                    where rvq.QuestionId == q.Id
                    where rvq.VoteId == voteId
                    select q).ToList();
        }

        public List<QuestionModel> LoadAvgMarks(int voteId)
        {
            using (VotesResultsEntities db = new VotesResultsEntities())
            {
                var questionModels = (
                    from rvqgs in db.RelVoteQuestionGroupSession
                    where rvqgs.VoteId == voteId
                    group rvqgs by rvqgs.QuestionId into g
                    select
                        new QuestionModel
                            {
                                Id = g.Key,
                                Content = db.Question.FirstOrDefault(qu => qu.Id == g.Key).Content,
                                Mark = (int)g.Average(q => q.Mark)
                            }).ToList();
                return questionModels;
            }
        }

        public List<QuestionModel> LoadActualQuestions(int voteId)
        {
            using (VotesResultsEntities db = new VotesResultsEntities())
                return db.RelVoteQuestionGroupSession.GroupBy(p => p.QuestionId)
                            .Select(g => new { Id = g.Key, AvgMark = g.Average(c => c.Mark) })
                            .OrderByDescending(x => x.AvgMark)
                            .AsEnumerable()
                            .Select(x =>
                                new QuestionModel
                                    {
                                        Id = x.Id,
                                        Content = db.Question.FirstOrDefault(q => q.Id == x.Id).Content,
                                        Mark = (float)x.AvgMark
                                    }).Take(5).ToList();
        }

        public Question Get(int id)
        {
            using (VotesResultsEntities db = new VotesResultsEntities())
                return db.Question.FirstOrDefault(m => m.Id == id);
        }

        public void Add(Question question)
        {
            using (VotesResultsEntities db = new VotesResultsEntities())
            {
                db.AddToQuestion(question);
                db.SaveChanges();
            }
        }

        public void Update(Question question)
        {
            using (VotesResultsEntities db = new VotesResultsEntities())
            {
                Question record = db.Question.FirstOrDefault(m => m.Id == question.Id);
                if (record != null) record.Content = question.Content;
                db.SaveChanges();
            }
        }

        public void Remove(int id)
        {
            using (VotesResultsEntities db = new VotesResultsEntities())
            {
                Question record = db.Question.FirstOrDefault(m => m.Id == id);
                if (record != null) db.DeleteObject(record);
                db.SaveChanges();
            }
        }
    }
}